/**
 *  This file is part of TaoBaoItemImporter.
 *
 *   TaoBaoItemImporter is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   TaoBaoItemImporter is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with TaoBaoItemImporter.  If not, see <http://www.gnu.org/licenses/>. 
 */
package com.googlecode.ulysses.taobao.synchronization;

import java.util.ArrayList;
import org.bson.types.*;

import com.googlecode.ulysses.taobao.BackDBWriter;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoException;

public class TjItemWriter implements BackDBWriter {

	private DBCollection srccoll;
	private DBCollection dstcoll;
	
	public TjItemWriter(DBCollection srccoll, DBCollection dstcoll){
		this.srccoll = srccoll;
		this.dstcoll = dstcoll;
	}

	public void updateCategoryDBs(String item_id, String item_name, ArrayList<Integer> cat_ids) throws MongoException{
		// TODO Auto-generated method stub
		BasicDBObject query = new BasicDBObject();
		query.put("_id", new ObjectId(item_id));
		query.put("item_name", item_name);
		
		//Result number, if count equal one we need insert child into sub_cat field;
		//if count equal zero, we create a new category document, otherwise println out error message 
		long count  = dstcoll.count(query);
		if(count == 1){
			BasicDBObject update = new BasicDBObject();
			
			for(Integer i:cat_ids)
			{
				update.put("$addToSet", new BasicDBObject("cat_ids", i));
			}
			
			dstcoll.update(query, update);
		}
		else if(count == 0){
			BasicDBObject doc = new BasicDBObject();
			doc.put("_id", new ObjectId(item_id));
			doc.put("item_name", item_name);
			doc.put("item_pop_val", 0.0);
			doc.put("cat_ids",cat_ids);
			
			//Add more fields into tj_items element, they are "rated_by_users":[ ],
			//"latent_features" : [ ], "similar_items" : [ ]
			ArrayList<Object> tmp = new ArrayList<Object>();
			doc.put("rated_by_users", tmp);
			doc.put("latent_features", tmp);
			doc.put("similar_items", tmp);
			
			dstcoll.insert(doc);
		}
		else {
			System.err.println(count + " elements have the same id and name in this collection");
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.googlecode.ulysses.taobao.BackDBWriter#writeCategoryDB()
	 */
	@Override
	public void writeCategoryDB(){
		DBCursor cursor = srccoll.find();
		BasicDBObject media = new BasicDBObject();
		String id;
		String name;
		ArrayList<Integer> child = new ArrayList<Integer>();
		while(cursor.hasNext()){
			media = (BasicDBObject)cursor.next();
			id = media.getString("_id");
			name = media.getString("item_name");
			child.clear();
			child.add(media.getInt("item_sub_cats"));
			child.add(media.getInt("item_main_cats"));
			
			updateCategoryDBs(id, name, child);
		}
	}
}
